package com.my.blog.website.config;


import com.my.blog.website.interceptor.BaseInterceptor;
import com.my.blog.website.utils.TaleUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 拦截器、静态资源
 *
 * @author 郭以华
 * @date 2019/3/9
 */
@Component
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private BaseInterceptor baseInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(baseInterceptor)
                .excludePathPatterns("/admin/css/**","/admin/images/**","/admin/js/**","/admin/layui/**","/admin/plugins/**")
                .excludePathPatterns("/user/css/**","/user/images/**","/user/js/**","/user/bootstrap/**","/user/editormd/**","/utils/**");
    }

    /**
     * 添加静态资源文件，外部可以直接访问地址
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            //如果是Windows系统
            registry.addResourceHandler("/carouselPic/**").
                    addResourceLocations("file:D:/cnbs/carousel/");
        } else {
            //linux和mac系统
            registry.addResourceHandler("/carouselPic/**").
                    addResourceLocations("file:/home/project/cnbs/upload/carousel/");
            registry.addResourceHandler("/upload/**").
                    addResourceLocations("file:" + TaleUtils.getUplodFilePath() + "upload/");
        }

    }

}
